package com.shujia.MR;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.List;

public class GetRes {
    static Connection connection;

    static {
        // 创建连接需要给定连接信息，所以需要给定配置类
        Configuration conf = new Configuration();

        // 由于对HBASE进行操作时，需要连接其Zookeeper 那么需要传入Zookeeper的IP
        conf.set("hbase.zookeeper.quorum", "node1,node2,master");
        // 获取连接对象 通过ConnectionFactory来创建连接对象

        try {
            connection = ConnectionFactory.createConnection(conf);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) throws IOException {

        // 对于数据进行操作，可以直接获取 TABLE 对象来操作
        TableName tableName = TableName.valueOf("api:countRes");
        Table table = connection.getTable(tableName);

        Scan scan = new Scan();

        // 通过Scanner扫描器得到的结果是一个Result的集合类
        ResultScanner scanner = table.getScanner(scan);

        for (Result result : scanner) {
            List<Cell> cells = result.listCells();
            String rowKey = Bytes.toString(result.getRow());
            for (Cell cell : cells) {

                String columns = Bytes.toString(CellUtil.cloneQualifier(cell));
                int num = Bytes.toInt(CellUtil.cloneValue(cell));
                System.out.println(rowKey+"=>"+columns+":"+num);
            }

        }


    }
}
